package com.sanshi.project.assets.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sanshi.project.admin.api.dto.LesseeDTO;
import com.sanshi.project.admin.api.entity.LesseeEntity;
import com.sanshi.project.assets.service.LesseeService;
import com.sanshi.project.common.core.util.R;
import com.sanshi.project.common.excel.annotation.ResponseExcel;
import com.sanshi.project.common.log.annotation.SysLog;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 承租户
 *
 * @author nikon
 * @date 2023-11-19 23:07:02
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/lessee")
@Tag(description = "lessee", name = "承租户管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class LesseeController {

    private final LesseeService lesseeService;

    /**
     * 分页查询
     *
     * @param page   分页对象
     * @param lessee 承租户
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('assets_lessee_view')")
    public R getLesseePage(@ParameterObject Page page, @ParameterObject LesseeDTO lessee) {
        LambdaQueryWrapper<LesseeEntity> wrapper = Wrappers.lambdaQuery();
        // 承租户类型
        wrapper.in(ArrayUtil.isNotEmpty(lessee.getType()), LesseeEntity::getType, lessee.getType());
        // 名称
        wrapper.like(StrUtil.isNotBlank(lessee.getName()), LesseeEntity::getName, lessee.getName());
        // 证件号码
        wrapper.like(StrUtil.isNotBlank(lessee.getCardNo()), LesseeEntity::getCardNo, lessee.getCardNo());
        // 联系方式
        wrapper.like(StrUtil.isNotBlank(lessee.getPhone()), LesseeEntity::getPhone, lessee.getPhone());
        // 性别
        wrapper.in(ArrayUtil.isNotEmpty(lessee.getSex()), LesseeEntity::getSex, lessee.getSex());
        // 联系地址
        wrapper.like(StrUtil.isNotBlank(lessee.getAddress()), LesseeEntity::getAddress, lessee.getAddress());
        // 法人姓名
        wrapper.like(StrUtil.isNotBlank(lessee.getCorporate()), LesseeEntity::getCorporate, lessee.getCorporate());
        // 法人身份证号
        wrapper.like(StrUtil.isNotBlank(lessee.getCorporateIdcard()), LesseeEntity::getCorporateIdcard, lessee.getCorporateIdcard());
        // 开户行
        wrapper.like(StrUtil.isNotBlank(lessee.getBankDeposit()), LesseeEntity::getBankDeposit, lessee.getBankDeposit());
        // 银行账号
        wrapper.like(StrUtil.isNotBlank(lessee.getBankAccount()), LesseeEntity::getBankAccount, lessee.getBankAccount());
        // 状态
        wrapper.in(ArrayUtil.isNotEmpty(lessee.getStatus()), LesseeEntity::getStatus, lessee.getStatus());
        wrapper.orderByDesc(LesseeEntity::getUpdateTime);
        return R.ok(lesseeService.page(page, wrapper));
    }


    /**
     * 通过id查询承租户
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('assets_lessee_view')")
    public R getById(@PathVariable("id") Long id) {
        return R.ok(lesseeService.getById(id));
    }

    /**
     * 新增承租户
     *
     * @param lessee 承租户
     * @return R
     */
    @Operation(summary = "新增承租户", description = "新增承租户")
    @SysLog("新增承租户")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('assets_lessee_add')")
    public R save(@RequestBody LesseeEntity lessee) {
        return R.ok(lesseeService.save(lessee));
    }

    /**
     * 修改承租户
     *
     * @param lessee 承租户
     * @return R
     */
    @Operation(summary = "修改承租户", description = "修改承租户")
    @SysLog("修改承租户")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('assets_lessee_edit')")
    public R updateById(@RequestBody LesseeEntity lessee) {
        return R.ok(lesseeService.updateById(lessee));
    }

    /**
     * 启动或者关闭编码承租户
     *
     * @param lessee 编码承租户表
     * @return R
     */
    @Operation(summary = "启动或者关闭承租户", description = "启动或者关闭承租户")
    @SysLog("启动或者关闭承租户")
    @PutMapping("/updateStatus")
    @PreAuthorize("@pms.hasPermission('assets_lessee_edit')")
    public R updateStatus(@RequestBody LesseeEntity lessee) {
        return R.ok(lesseeService.updateById(lessee));
    }


    /**
     * 通过id删除承租户
     *
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除承租户", description = "通过id删除承租户")
    @SysLog("通过id删除承租户")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('assets_lessee_del')")
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(lesseeService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     *
     * @param lessee 查询条件
     * @param ids    导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('assets_lessee_export')")
    public List<LesseeEntity> export(LesseeEntity lessee, Long[] ids) {
        return lesseeService.list(Wrappers.lambdaQuery(lessee).in(ArrayUtil.isNotEmpty(ids), LesseeEntity::getId, ids));
    }

    /**
     * 承租户全量查询
     *
     * @param lessee 承租户管理
     * @return
     */
    @Operation(summary = "承租户管理查询", description = "承租户管理查询")
    @GetMapping("/list")
    @PreAuthorize("@pms.hasPermission('assets_lessee_view')")
    public R getLesseeList(@ParameterObject LesseeDTO lessee) {
        LambdaQueryWrapper<LesseeEntity> wrapper = Wrappers.lambdaQuery();
        // 承租户类型
        wrapper.in(ArrayUtil.isNotEmpty(lessee.getType()), LesseeEntity::getType, lessee.getType());
        // 名称
        wrapper.like(StrUtil.isNotBlank(lessee.getName()), LesseeEntity::getName, lessee.getName());
        // 证件号码
        wrapper.like(StrUtil.isNotBlank(lessee.getCardNo()), LesseeEntity::getCardNo, lessee.getCardNo());
        // 联系方式
        wrapper.like(StrUtil.isNotBlank(lessee.getPhone()), LesseeEntity::getPhone, lessee.getPhone());
        // 性别
        wrapper.in(ArrayUtil.isNotEmpty(lessee.getSex()), LesseeEntity::getSex, lessee.getSex());
        // 联系地址
        wrapper.like(StrUtil.isNotBlank(lessee.getAddress()), LesseeEntity::getAddress, lessee.getAddress());
        // 法人姓名
        wrapper.like(StrUtil.isNotBlank(lessee.getCorporate()), LesseeEntity::getCorporate, lessee.getCorporate());
        // 法人身份证号
        wrapper.like(StrUtil.isNotBlank(lessee.getCorporateIdcard()), LesseeEntity::getCorporateIdcard, lessee.getCorporateIdcard());
        // 开户行
        wrapper.like(StrUtil.isNotBlank(lessee.getBankDeposit()), LesseeEntity::getBankDeposit, lessee.getBankDeposit());
        // 银行账号
        wrapper.like(StrUtil.isNotBlank(lessee.getBankAccount()), LesseeEntity::getBankAccount, lessee.getBankAccount());
        // 状态
        wrapper.in(ArrayUtil.isNotEmpty(lessee.getStatus()), LesseeEntity::getStatus, lessee.getStatus());
        wrapper.orderByDesc(LesseeEntity::getUpdateTime);
        return R.ok(lesseeService.list(wrapper));
    }
}